package client;

import server.netty.GpcDataPackage;
import view.TankView;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @ClassName:       GpcResponseReader
 * @Description:     TODO GPC响应监听器
 * @author:          ljh
 * @version:         CMR--
 * @date:            2019年3月22日        下午2:20:05
 */
public class GpcResponseListener {

	private final ConcurrentMap<String,TankObserver> observers = new ConcurrentHashMap<String, TankObserver>();
	private static final GpcResponseListener listener = new GpcResponseListener();
	private TankClient client;
	private int index = 0;
	
	public static GpcResponseListener getInstance(TankClient client) {
		listener.setClient(client);
		return listener;
	}
	
	public void setClient(TankClient client) {
		this.client = client;
	}
	
	public final void response(GpcDataPackage response) {
		String id = response.getUnique();
		TankObserver observer = observers.getOrDefault(id, null);
		if(observer == null) {
			observer = new TankViewObserver(new TankView(this.client));
			observers.putIfAbsent(id, observer);
		} 
		if(response.getStatus() == 200) {
			client.setClientId(id);
		} else if(response.getStatus() == 11){
			
		} else if(response.getStatus() == 10) {
			//client.getClient().sendMessage(response);
		}
	}
}
